1 // Debugging iterator implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file debug/safe_iterator.tcc
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
31 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
35 template<typename _Iterator, typename _Sequence>
37 _Safe_iterator<_Iterator, _Sequence>::
38 _M_can_advance(const difference_type& __n) const
40 typedef typename _Sequence::const_iterator const_iterator;
42 if (this->_M_singular())
48 const_iterator __begin =
49 static_cast<const _Sequence*>(_M_sequence)->begin();
50 std::pair<difference_type, _Distance_precision> __dist =
51 this->_M_get_distance(__begin, *this);
52 bool __ok = ((__dist.second == __dp_exact && __dist.first >= -__n)
53 || (__dist.second != __dp_exact && __dist.first > 0));
58 const_iterator __end =
59 static_cast<const _Sequence*>(_M_sequence)->end();
60 std::pair<difference_type, _Distance_precision> __dist =
61 this->_M_get_distance(*this, __end);
62 bool __ok = ((__dist.second == __dp_exact && __dist.first >= __n)
63 || (__dist.second != __dp_exact && __dist.first > 0));
68 template<typename _Iterator, typename _Sequence>
69 template<typename _Other>
71 _Safe_iterator<_Iterator, _Sequence>::
72 _M_valid_range(const _Safe_iterator<_Other, _Sequence>& __rhs) const
74 if (!_M_can_compare(__rhs))
77 /* Determine if we can order the iterators without the help of
79 std::pair<difference_type, _Distance_precision> __dist =
80 this->_M_get_distance(*this, __rhs);
81 switch (__dist.second) {
83 if (__dist.first == 0)
89 return __dist.first >= 0;
92 /* We can only test for equality, but check if one of the
93 iterators is at an extreme. */
94 if (_M_is_begin() || __rhs._M_is_end())
96 else if (_M_is_end() || __rhs._M_is_begin())
99 // Assume that this is a valid range; we can't check anything else
103 template<typename _Iterator, typename _Sequence>
105 _Safe_iterator<_Iterator, _Sequence>::
108 __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
109 _M_invalidate_single();
112 template<typename _Iterator, typename _Sequence>
114 _Safe_iterator<_Iterator, _Sequence>::
115 _M_invalidate_single()
117 typedef typename _Sequence::iterator iterator;
118 typedef typename _Sequence::const_iterator const_iterator;
120 if (!this->_M_singular())
122 for (_Safe_iterator_base* __iter = _M_sequence->_M_iterators;
123 __iter; __iter = __iter->_M_next)
125 iterator* __victim = static_cast<iterator*>(__iter);
126 if (this->base() == __victim->base())
127 __victim->_M_version = 0;
130 for (_Safe_iterator_base* __iter2 = _M_sequence->_M_const_iterators;
131 __iter2; __iter2 = __iter2->_M_next)
133 const_iterator* __victim = static_cast<const_iterator*>(__iter2);
134 if (__victim->base() == this->base())
135 __victim->_M_version = 0;
140 } // namespace __gnu_debug